package be.djdb.mdi;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class MYMDI extends JDesktopPane implements Mydecorateblemdi{
	private static final long serialVersionUID = 4080749742822235380L;
	private int mdiHeight;
	private int mdiWidh;
	private int mdiOffset=20;
	

	/**
	 * <%@ page contentType="text/html; charset=" %>
	<pre>
	import be.djdb.generators.Menu;
	public class mditest  extends JFrame {
			private MYMDI desktop = new MYMDI();
			private JMenuBar menuBar ;
			private JScrollPane scrollPane = new JScrollPane();
			public mditest() {
				Menu menu= new Menu("test");
				menu.addmenuitem("File", "New",new JMenuItem(),new ActionListener() {
					public void actionPerformed(ActionEvent ae) {
						System.out.println("add dekstop");
						desktop.add(new Testframe());
					}
				});
				menuBar = menu.getmenubar();
				menuBar.add(new Mywindowmenu(desktop));
				setJMenuBar(menuBar);
				scrollPane.getViewport().add(desktop);
				getContentPane().setLayout(new BorderLayout());
				getContentPane().add(scrollPane, BorderLayout.CENTER);
			}
			public static void main(String[] args) {
				mditest notepad = new mditest();
				notepad.setSize(600, 400);
				notepad.setVisible(true);
			}
		}
		</pre>
	 */	
	public MYMDI() {
		 mdiHeight = 0;
		 mdiWidh = 0;
	}

	@Override
	public JInternalFrame[] getFrames() {
		return super.getAllFrames();
	}
	@Override
	public int getFramseCount() {
		return super.getComponentCount();//getAllFrames.length
	}
	/**
	 * 
	 */
	public void resize(){
		 JScrollPane scrollPane = null ;//getScrollPane();
		 //Insets scrollInsets = getScrollPaneInsets();
		 if (scrollPane != null) {
		   JInternalFrame[] allFrames = this.getFrames();
		   for (int id = 0; id < allFrames.length; id++) {
				 JInternalFrame frame =allFrames[id];
				 setminsize(frame.getX(),frame.getY(),frame.getWidth(),frame.getHeight());
		   }
		   Dimension d = scrollPane.getVisibleRect().getSize();
		   if (scrollPane.getBorder() != null) {
		    // d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right, d.getHeight()
		    //     - scrollInsets.top - scrollInsets.bottom);
		   }
		   if (mdiWidh <= d.getWidth())
			   mdiWidh = ((int) d.getWidth()) - 20;
		   if (mdiHeight <= d.getHeight())
			   mdiHeight = ((int) d.getHeight()) - 20;
		   setallmySize(new Dimension(mdiWidh, mdiHeight));
	  }
	}
			public void setallmySize(Dimension d) {
				setMinimumSize(d);
				setMaximumSize(d);
				setPreferredSize(d);
			}
			public void setminsize(int x,int y,int with,int height){
				 if (x + with > mdiWidh) {
					 mdiWidh = x + with;
			     }
			     if (y + height > y) {
			       y = y + height;
			     }
			}
			@Override
			public void setFrames_tile() {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void setFrames_cascade() {
				// TODO Auto-generated method stub
				
			}
			/**
			 * <%@ page contentType="text/html; charset=" %>
			 * @param frame
			 * <pre>
			 public class Testframe extends JInternalFrame {
			 			private JTextArea textArea = new JTextArea("testarea");
			 			private JScrollPane scrollPane = new JScrollPane();
			 			private boolean debug = true;
			 			public Testframe() {
			 				if (debug ){System.out.println("<start>");}
			 				setSize(50,50);
			 				setTitle("Edit Text");
			 				setMaximizable(true);
			 				setIconifiable(true);
			 				setClosable(true);
			 				setResizable(true);
			 				scrollPane.getViewport().add(textArea);
			 				getContentPane().setLayout(new BorderLayout());
			 				getContentPane().add(scrollPane, BorderLayout.CENTER);
			 			}
			 }</pre>
			 */
			public Component add(JInternalFrame frame) {
				JInternalFrame[] array = getFrames();
				Point p;
				int w =30;
				int h =30;
				Component retval = super.add(frame);
				if (getParent() != null && isVisible()){
					resize();
				}
				if (array.length > 0) {
					p = array[0].getLocation();
					p.x = p.x + mdiOffset;
					p.y = p.y + mdiOffset;
				} else {
					p = new Point(0, 0);
				}
				frame.setLocation(p.x, p.y);
				if (frame.isResizable()) {
					if (w < frame.getMinimumSize().getWidth())
						w = (int) frame.getMinimumSize().getWidth();
					if (h < frame.getMinimumSize().getHeight())
						h = (int) frame.getMinimumSize().getHeight();
					frame.setSize(w, h);
				}
				moveToFront(frame);
				frame.setVisible(true);
				try {
					frame.setSelected(true);
				} catch (PropertyVetoException e) {
					frame.toBack();
				}
				return retval;
			}
}
